package com.brt.museum.module;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.SetupBy;
import org.nutz.mvc.annotation.Views;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.mvc.ioc.provider.ComboIocProvider;

import com.brt.museum.setup.ApplicationSetup;
import com.brt.museum.vo.Consts.SessionKeys;
import com.dgj.nutz.module.base.AbstractBaseModule;
import com.dgj.nutz.velocity.VelocityViewMaker;
import com.dgj.shiro.ShiroActionFilter;
import com.dgj.utils.entries.Result;

/**
 * @author 王贵源
 * @email kerbores@dgj.com
 *
 *        create at 2015年10月19日 下午3:51:40
 */
@Modules(scanPackage = true)
@IocBy(type = ComboIocProvider.class, args = { "*js", "ioc", "*anno", "com.dgj", "com.brt", "*tx", "*com.dgj.quartz.QuartzIocLoader" })
// 全局失败视图
@Fail("http:500")
// 默认json渲染
@Ok("json")
// 添加velocity视图
@Views(VelocityViewMaker.class)
@SetupBy(ApplicationSetup.class)
// shiro的控制器拦截
@Filters({ @By(type = CheckSession.class, args = { SessionKeys.SESSION_USER_KEY, "/" }), @By(type = ShiroActionFilter.class, args = "/") })
public class MainModule extends AbstractBaseModule {

	@Inject
	Dao dao;

	/**
	 * 数据源测试
	 * 
	 * @return
	 */
	@At
	public Result db() {
		return Result.success().addData("db", dao.meta()).addData("sqls", dao.sqls().count()).addData("sql1", dao.sqls().get("sql1"));
	}

	@At
	@Ok("vel:test/test.html")
	public Result test() {
		return Result.success();
	}

	/**
	 * hello world
	 * 
	 * @return
	 */
	@At
	public String hello() {
		return "Hello World!";
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@At
	public Result client(HttpServletRequest request) {
		return Result.success().addData("ip", _ip()).addData("ua", _ua());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.nutz.module.base.AbstractBaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return "main";
	}

	@At("/")
	@Ok("->:/system/login")
	@Filters
	public Result main() {
		return Result.success();
	}
}
